package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 牛俊杰
 */
@Service
public class ProductServiceImpl implements ProductService {

  @Autowired
  private ProductMapper productMapper;

  @Autowired
  private ProductStockMapper productStockMapper;

  /**
   * 查询所有物资信息列表
   * @param pageNum
   * @param pageSize
   * @param productVO
   * @return
   */
  @Override
  public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, ProductVO productVO) {
    PageHelper.startPage(pageNum,pageSize);
    List<Product> products;
    Example p = new Example(Product.class);
    Example.Criteria criteria = p.createCriteria();
    if (productVO.getStatus()!=null){
      criteria.andEqualTo("status",productVO.getStatus());
    }
    if (productVO.getThreeCategoryId()!=null){
      criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
      .andEqualTo("twoCategoryId",productVO.getTwoCategoryId())
      .andEqualTo("threeCategoryId",productVO.getThreeCategoryId());
      products = productMapper.selectByExample(p);
      List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
      PageInfo<Product> info = new PageInfo<>(products);
      return new PageVO<>(info.getTotal(),productVOS);
    }
    if (productVO.getThreeCategoryId()!=null){
      criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
      .andEqualTo("twoCategoryId",productVO.getTwoCategoryId());
      products = productMapper.selectByExample(p);
      List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
      PageInfo<Product> info = new PageInfo<>(products);
      return new PageVO<>(info.getTotal(),productVOS);
    }
    if (productVO.getThreeCategoryId()!=null){
      criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId());
      products = productMapper.selectByExample(p);
      List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
      PageInfo<Product> info = new PageInfo<>(products);
      return new PageVO<>(info.getTotal(),productVOS);
    }
    p.setOrderByClause("sort asc");
    if (productVO.getName()!=null&&!"".equals(productVO.getName())){
      criteria.andLike("name","%" + productVO.getName() + "%");
    }
    products = productMapper.selectByExample(p);
    List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
    PageInfo<Product> info = new PageInfo<>(products);
    return new PageVO<>(info.getTotal(),productVOS);
  }

  /**
   * 添加物资信息
   * @param productVO
   */
  @Override
  public void add(ProductVO productVO) {
    Product product = new Product();
    BeanUtils.copyProperties(productVO,product);
    product.setCreateTime(new Date());
    product.setModifiedTime(new Date());
    @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
    if (categoryKeys.length==3){
      product.setOneCategoryId(categoryKeys[0]);
      product.setTwoCategoryId(categoryKeys[1]);
      product.setThreeCategoryId(categoryKeys[2]);
    }
    product.setStatus(2);
    product.setPNum(UUID.randomUUID().toString().substring(0,32));
    productMapper.insert(product);
  }

  /**
   * 编辑物资信息页面
   * @param id
   * @return
   * @throws BusinessException
   */
  @Override
  public ProductVO edit(Long id) throws BusinessException {
    Product product = productMapper.selectByPrimaryKey(id);
    if (product==null){
      throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资信息不存在");
    }
    return ProductConverter.converterToProductVO(product);
  }

  /**
   * 修改物资信息
   * @param id
   * @param productVO
   */
  @Override
  public void update(Long id, ProductVO productVO) {
    Product product = new Product();
    BeanUtils.copyProperties(product,productVO);
    product.setModifiedTime(new Date());
    @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
    if (categoryKeys.length==3){
      product.setOneCategoryId(categoryKeys[0]);
      product.setTwoCategoryId(categoryKeys[1]);
      product.setThreeCategoryId(categoryKeys[2]);
    }
    productMapper.updateByPrimaryKey(product);
  }

  /**
   * 将物资信息移动至回收站
   * @param id
   * @throws BusinessException
   */
  @Override
  public void remove(Long id) throws BusinessException {
    Product product = new Product();
    product.setId(id);
    Product p = productMapper.selectByPrimaryKey(product);
    if (p.getStatus()!=0){
      throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
    }
    product.setStatus(1);
    productMapper.updateByPrimaryKeySelective(product);
  }

  /**
   * 恢复回收站中的物资信息
   * @param id
   * @throws BusinessException
   */
  @Override
  public void back(Long id) throws BusinessException {
    Product product = new Product();
    product.setId(id);
    Product p = productMapper.selectByPrimaryKey(product);
    if (p.getStatus()!=1){
      throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
    }
    product.setStatus(0);
    productMapper.updateByPrimaryKeySelective(product);
  }

  /**
   * 删除物资信息
   * @param id
   * @throws BusinessException
   */
  @Override
  public void delete(Long id) throws BusinessException {
    Product product = new Product();
    product.setId(id);
    Product p = productMapper.selectByPrimaryKey(product);
    //只可删除待审核或在回收站中的物资信息
    if (p.getStatus()!=1&&p.getStatus()!=2){
      throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
    }
    productMapper.deleteByPrimaryKey(product);
  }

  /**
   * 审核通过物资信息
   * @param id
   * @throws BusinessException
   */
  @Override
  public void publish(Long id) throws BusinessException {
    Product product = new Product();
    product.setId(id);
    Product p = productMapper.selectByPrimaryKey(product);
    if (p.getStatus()!=2){
      throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
    }
    product.setStatus(0);
    productMapper.updateByPrimaryKeySelective(product);
  }

  /**
   * 物资库存
   * @param pageNum
   * @param pageSize
   * @param productVO
   * @return
   */
  @Override
  public PageVO<ProductStockVO> findProductStocks(Integer pageNum, Integer pageSize, ProductVO productVO) {
    PageHelper.startPage(pageNum,pageSize);
    List<ProductStockVO> productStockVOList = productStockMapper.findProductStocks(productVO);
    PageInfo<ProductStockVO> info = new PageInfo<>(productStockVOList);
    return new PageVO<>(info.getTotal(),productStockVOList);
  }

  @Override
  public List<ProductStockVO> findAllStocks(Integer pageNum, Integer pageSize, ProductVO productVO) {
    PageHelper.startPage(pageNum,pageSize);
    return productStockMapper.findAllStocks(productVO);
  }
}
